package View;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.TreeSet;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Custom.AccountLayout;
import Model.Account;
import Model.Accounts;
import Model.PieChart;

@SuppressWarnings("serial")
public class PanelAccountView extends JPanel{
	
	private static PanelAccountView single;

	private JLabel title = new JLabel("Rekeningen overzicht");
	
	private JButton confirmButton = new JButton("Toevoegen");
	private JButton deleteButton = new JButton("Verwijderen");
	
	private Color backcolor = new Color(239, 238, 237);
	private Color titleColor = new Color(85, 85, 85);
	
	private int marginLeft =  -25;
	private int marginTop =  370;


	private PanelAccountView(){
		
		setLayout(null);
		setBounds(140, 0, 860, 725);
		
		PieChart p = new PieChart(null);
		p.setBounds(100, 60, 640, 300);
		add(p);


	    System.out.println(Accounts.getInstance().getAccount());
	    
	    Iterator<Account> itr = Accounts.getInstance().getAccount().iterator();
	    
	   
	    while(itr.hasNext()){
	    	Account a = itr.next();
	    	System.out.println("hoi");
	    	dynamicMarginLeft();
	    	add(new AccountLayout(a, marginLeft, marginTop));
		}
	    

		setBackground(backcolor);

		
	    title.setBounds(330, 20, 200, 30);
	    title.setForeground(titleColor);
	    title.setFont(new Font("Serif", Font.BOLD, 20));
		
		confirmButton.setBounds(525, 650, 115, 30);
		confirmButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				PanelHolder mf = PanelHolder.getInstance();
				mf.remove(PanelAccountView.getInstance());
				mf.add(PanelAddAccount.getInstance());
				mf.repaint();
				mf.validate();
			}
		});
		
		deleteButton.setBounds(650, 650, 115, 30);
		
		add(title);
		add(confirmButton);
		add(deleteButton);

	}
	
	
	public void dynamicMarginLeft() {
		marginLeft = (marginLeft + 128);
		if(marginLeft > 640){
			marginTop = marginTop + 100;
			marginLeft = 100;
		}
	}



	public static PanelAccountView getInstance() {
		if(single == null){
			single = new PanelAccountView();
		}
		return single;
	}
	
	public static void selfDestroy(){
		single = null;
	}
	
}
